    $(function() {
        $("#journal_tree")
            .jstree({
                json_data: {
                    "ajax": {
                        "url": "GetJournalData",
                        "type": "POST",
                        "dataType": "json",
                        "contentType": "application/json charset=utf-8"
                    }
                },
                plugins: ["themes", "json_data", "ui", "crrm", "dnd", "types", "contextmenu"],
                types: {
                    // I set both options to -2, as I do not need depth and children count checking
                    // Those two checks may slow jstree a lot, so use only when needed
                    "max_depth": -2,
                    "max_children": -2,
                    // I want only `pupil_class` nodes to be root nodes 
                    // This will prevent moving or creating any other type as a root node
                    "valid_children": ["pupil_class"],
                    "types": {
                        // The default type
                        "default": {
                            // I want this type to have no children (so only leaf nodes)
                            // In my case - those are files
                            "valid_children": "none",
                            // If we specify an icon for the default type it WILL OVERRIDE the theme icons
                            "icon": { "image": "../../Images/file.png" }
                        },
                        // The `pupil` type
                        "pupil": {
                            // can have files and other pupils inside of it, but NOT `pupil_class` nodes
                            "valid_children": "none",
                            "icon": { "image": "../../Images/pupil.png" },
                            "rename": false,
                            "start_drag": false
                        },
                        "teacher": {
                            // can have files and other pupils inside of it, but NOT `pupil_class` nodes
                            "valid_children": "none",
                            "icon": { "image": "../../Images/teacher.png" },
                            "rename": false,
                        },
                        "teachers": {
                            "valid_children": ["teacher", "default"],
                            "icon": { "image": "../../Images/folder.png" },
                            "rename": false,
                            "start_drag": false
                        },
                        "parent": {
                            // can have files and other pupils inside of it, but NOT `pupil_class` nodes
                            "valid_children": "none",
                            "icon": { "image": "../../Images/parent.png" },
                            "rename": false,
                        },
                        "parents": {
                            "valid_children": ["parent", "default"],
                            "icon": { "image": "../../Images/folder.png" },
                            "rename": false,
                            "start_drag": false
                        },
                        "users_container": {
                            "valid_children": ["default"],
                            "icon": { "image": "../../Images/folder.png" },
                            "rename": false,
                        },
                        // The `pupil_class` nodes 
                        "pupil_class": {
                            // can have files and pupils inside, but NOT other `pupil_class` nodes
                            "valid_children": ["default", "pupil"],
                            "icon": { "image": "../../Images/journal.png" },
                            // those prevent the functions with the same name to be used on `pupil_class` nodes
                            // internally the `before` event is used
                            "start_drag": false,
                            "move_node": false,
                            //"delete_node": false,
                            //"remove": false
                        },
                        "subjects": {
                            // can have files and other pupils inside of it, but NOT `pupil_class` nodes
                            "valid_children": "subject",
                            "icon": { "image": "../../Images/subjects.png" },
                            "rename": false,
                            "start_drag": false,
                        },
                        "subject": {
                            // can have files and other pupils inside of it, but NOT `pupil_class` nodes
                            "valid_children": "none",
                            "icon": { "image": "../../Images/subject.png" },
                            "rename": false,
                            "start_drag": false,
                        },
                        "timetable": {
                            // can have files and other pupils inside of it, but NOT `pupil_class` nodes
                            "valid_children": "timetable_dayofweek",
                            "icon": { "image": "../../Images/timetable.png" },
                            "rename": false,
                            "start_drag": false,
                        },
                        "timetable_dayofweek": {
                            // can have files and other pupils inside of it, but NOT `pupil_class` nodes
                            "valid_children": "none",
                            "icon": { "image": "../../Images/timetable_dayofweek.png" },
                            "rename": false,
                            "start_drag": false,
                        },
                    }
                },
                //}
                //,
                //callback: {
                //    onselect: function(node, tree) {
                //        var aa = 0;
                //    },
                //    onload: function (tree) {
                //        $('li[selected=true]').each(function () {
                //            $.tree.plugins.checkbox.check(this);
                //        });
                //    },
                //    onclick: function (e) {
                //        debugger;
                //        var ii = 0;
                //    }
                //},
            })
            .bind("create.jstree", function (e, data) {
                debugger;
                $.ajax({
                    url: "AddNewNode",
                    type: "POST",
                    data: {
                        "nodeName": data.rslt.name,
                        "parentNodeType": data.rslt.parent == -1 ? "pupil_class" : data.rslt.parent[0].id,
                    },
                    success: function() {
                        data.inst.refresh();
                    },
                    error: function () {
                        data.inst.refresh();
                    }
                });
            })
            .bind("remove.jstree", function(e, data) {
                debugger;
                data.rslt.obj.each(function() {
                    $.ajax({
                        url: "RemoveNode",
                        type: "POST",
                        data: {
                            "nodeType": data.rslt.obj.attr("rel"),
                            "nodeName": data.rslt.obj[0].id
                        },
                        success: function(r) {
                            if (!r.status) {
                                data.inst.refresh();
                            }
                        }
                    });
                });
            })
            .bind("rename.jstree", function (e, data) {
                debugger;
                $.ajax({
                    url: "RenameNode",
                    type: "POST",
                    data: {
                        "nodeType": data.rslt.obj.attr("rel"),
                        "nodeId": data.rslt.obj[0].id,
                        "newNodeName": data.rslt.new_name
                    },
                    //success: function(r) {
                    //    if (!r.status) {
                    //        $.jstree.rollback(data.rlbk);
                    //    }
                    //}
                });
            })
            .bind("select_node.jstree", function(e, data) {
                debugger;
                $.ajax({
                    url: "NodeDetails",
                    type: "GET",
                    dataType: "html",
                    data: {
                        "id": data.rslt.obj[0].id,
                        "nodeType": data.rslt.obj.attr("rel"),
                    },
                    success: function(data) {
                        $("#user_details").empty().html(data);
                    }
                });
            })
            .bind("move_node.jstree", function(e, data) {
                debugger;
                data.rslt.o.each(function(i) {
                    $.ajax({
                        async: false,
                        type: 'POST',
                        url: "MoveNode",
                        data: {
                            "userDetailsId": $(this).attr("id"),
                            "toNodeType": data.rslt.np.attr("rel"),
                            "toNodeName": data.rslt.np.attr("id"),
                            //"to": data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_", ""),
                            //"position": data.rslt.cp + i,
                            //"title": data.rslt.name,
                            //"copy": data.rslt.cy ? 1 : 0
                        },
                        //success: function(r) {
                        //    data.inst.refresh();
                        //},
                        complete: function (r) {
                            data.inst.refresh();
                        },
                        
                    });
                });
            });
    });
    
    // Code for the menu buttons
    $(function() {
        $("#mmenu input").click(function() {
            switch (this.id) {
            case "add_pupil_class":
                $("#journal_tree").jstree("create", 0, "last", { "attr": { "rel": this.id.toString().replace("add_", "") } });
                break;
            case "add_user":
                debugger;
                $.ajax({
                    url: "NodeDetails",
                    type: "GET",
                    dataType: "html",
                    data: {
                        "id": "0", // hard-coded
                        "nodeType": "register_new_user",
                    },
                    success: function (data) {
                        $("#user_details").empty().html(data);
                        debugger;
                    }
                });
                break;

            case "add_default":
                //case "add_pupil":
                //    $("#journal_tree").jstree("create", null, "last", { "attr": { "rel": this.id.toString().replace("add_", "") } });
                break;
            //case "search":
            //    $("#journal_tree").jstree("search", document.getElementById("text").value);
            //    break;
            //case "text":
            //    break;
            default:
                $("#journal_tree").jstree(this.id);
                break;
            }
        });
    });